package game;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.event.KeyEvent;
import java.awt.geom.AffineTransform;

import loading.ImageLoader;
import math.Vector2D;

import user.Button;
import user.User;
import gamestate.State;
import gamestate.StateStack;
import gui.GUI;

public class MenuState extends State
{
	private ImageLoader title;
	
	private Vector2D[] velocities;
	private ImageLoader[] images;
	private int index;
	private Vector2D pos;
	
	private double timeOnIndex;
	
	private float alpha;
	
	private Button singleplayer, multiplayer, exit;
	
	public MenuState( StateStack s, User u )
	{
		super( s, u );
		
		title = new ImageLoader( "Images/ImagesforMenu/title.png" );
		
		images = new ImageLoader[8];
		velocities = new Vector2D[8];

		images[0] = new ImageLoader( "Images/ImagesforMenu/end4.jpg" );
		images[1] = new ImageLoader( "Images/ImagesforMenu/archangel_raphael.jpg" );
		images[2] = new ImageLoader( "Images/ImagesforMenu/bg-desert.jpg" );
		images[3] = new ImageLoader( "Images/ImagesforMenu/End_of_the_world_by_Ninja_Ralph.jpg" );
		images[4] = new ImageLoader( "Images/ImagesforMenu/Michael.jpg" );
		images[5] = new ImageLoader( "Images/ImagesforMenu/the-four-horsemen-of-the-apocalypse.jpg" );
		images[6] = new ImageLoader( "Images/ImagesforMenu/US_Military_Formation.jpg" );
		images[7] = new ImageLoader( "Images/ImagesforMenu/thetriumphofdeath.jpg" );

		velocities[0] = new Vector2D( 0, 10 );
		velocities[1] = new Vector2D( 0, -15 );
		velocities[2] = new Vector2D( 0, 10 );
		velocities[3] = new Vector2D( 0, -10 );
		velocities[4] = new Vector2D( 0, -10 );
		velocities[5] = new Vector2D( 15, 10 );
		velocities[6] = new Vector2D( 0, -23 );
		velocities[7] = new Vector2D( 10, -10 );
		
		pos = new Vector2D();
		
		alpha = 1.0f;
		
		index = 0;

		singleplayer = new Button( new Vector2D( 1024 / 2 - 66, 200 ), new ImageLoader( "Images/ImagesforMenu/singleplayer.png" ) );
		singleplayer.setEnabled( false );
		multiplayer = new Button( new Vector2D( 1024 / 2 - 66, 300 ), new ImageLoader( "Images/ImagesforMenu/multiplayer.png" ) );
		exit = new Button( new Vector2D( 1024 / 2 - 66, 400 ), new ImageLoader( "Images/ImagesforMenu/exit.png" ) );
	}

	public void render( Graphics2D g )
	{
		g.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, alpha ) );
		
		images[index].draw( g, AffineTransform.getTranslateInstance( GUI.getInstance().getWidth() / 2 - images[index].width() / 2 + pos.getX(), pos.getY() ) );
		
		g.setComposite( AlphaComposite.getInstance( AlphaComposite.SRC_OVER, 1.0f ) );
		
		g.setColor( Color.black );
		
		g.fillRect( 0, 0, 1024, 128 );
		g.fillRect( 0, 640, 1024, 128 );
		
		g.setColor( new Color( 1.0f, 1.0f, 1.0f, 0.5f ) );
		
		g.fillRect( GUI.getInstance().getWidth() / 2 - 200, 128, 400, 512 );
		
		title.draw( g, AffineTransform.getTranslateInstance( GUI.getInstance().getWidth() / 2 - title.width() / 2, -7 ) );
		
		singleplayer.render( g );
		multiplayer.render( g );
		exit.render( g );
	}

	public void update( double duration )
	{
		Vector2D mouse = getUser().getMousePos();
		if( mouse != null )
		{
			if( singleplayer.click( mouse ) )
			{
			
			}
			else if( multiplayer.click( mouse ) )
			{
				
			}
			else if( exit.click( mouse ) )
			{
				System.exit( 0 );
			}
			
			getUser().doneWithMouse();
		}
		
		pos.add( Vector2D.multiply( velocities[index], duration ) );
		
		timeOnIndex += duration;
		
		if( timeOnIndex < 1.2 )
		{
			alpha += 0.01f;
			if( alpha > 1.0f )
				alpha = 1.0f;
		}
		
		if( timeOnIndex > 5.8 )
		{
			alpha -= 0.01f;

			if( alpha < 0.0f )
				alpha = 0.0f;
		}
		
		if( timeOnIndex > 7 )
		{
			index++;
			if( index == images.length )
				index = 0;
			
			timeOnIndex = 0;
			
			pos.setX( 0 ); pos.setY( 0 );
		}
	}
}
